package com.lanou.dllo.soccernews.fragment.circle2fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.ShowCircleAdapter;
import com.lanou.dllo.soccernews.bean.CircleShowBean;
import com.lanou.dllo.soccernews.bean.DeepBean;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class Circle2Fragment extends Fragment {
    private ExpandableListView mExpandableListView;
    private ShowCircleAdapter adapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(R.layout.fragment_circle2, null);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mExpandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);



    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addData();
        // Log.i("lanou", "加载布局");

        mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener
                () {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });


        mExpandableListView
                .setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent,
                                                View v, int groupPosition, int childPosition,
                                                long id) {
//                        Log.e("lanpo", "groupPosition=" + groupPosition
//                                + ",childPosition=" + childPosition);
                        return false;
                    }
                });
    }


    //添加数据

    private void addData() {
        String url = "https://api.dongqiudi.com/leagues/groups";
        //   Log.i("lanou", "加载布局"+url);
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        /*
                        *  List<EuropeanCupBean> ec = gson.fromJson(response, new TypeToken<List<EuropeanCupBean>>() {
                        }.getType());*/
                        List<CircleShowBean> co = gson.fromJson(response,
                                new TypeToken<List<CircleShowBean>>() {
                                }.getType());
                      //  Log.i("lanou", co + "");
                        adapter = new ShowCircleAdapter(getActivity());
                        mExpandableListView.setAdapter(adapter);
                        mExpandableListView.expandGroup(0);
                        adapter.addDatas(co);


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }
}
